package page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import abstractclass.PageManagerAbstract;

public class PageManagerMain extends PageManagerAbstract {
	private List context = null;
	private String[] titles = null;
	private String[] dbtitles = null;
	
	public void setDbtitles(String[] dbtitles) {
		this.dbtitles = dbtitles;
	}


	public void setTitles(String[] titles) {
		this.titles = titles;
	}


	private int pagecount = 10;
	
	public PageManagerMain(List context) {
		// TODO Auto-generated constructor stub
		this.context = context;
	}
	

	@Override
	public List getPage(int page) {
		// TODO Auto-generated method stub
		if(context == null){
			return null;
		}
		
		if(context.size() <= page*pagecount){
			return null;
		}
		
		List<Object> retList  = new ArrayList<Object>();
		
		int total = context.size() - page*pagecount;
		if(total >10){
			total = 10;
		}
		
		for(int i=0;i<total;i++){
			retList.add(context.get(page*pagecount+i));
		}
		
		return retList;
	}


	@Override
	public String[] getTableTitle() {
		// TODO Auto-generated method stub
		return titles;
	}


	@Override
	public String[] getDBTitles() {
		// TODO Auto-generated method stub
		return dbtitles;
	}


	@Override
	public String[] getLines(int page,int line) {
		// TODO Auto-generated method stub
		List list = getPage(page);
		if(list==null){
			return null;
		}
		
		HashMap<String, Object> maps = (HashMap<String, Object>) list.get(line);
		String[] lineStrings = new String[titles.length];
		for(int i=0;i<titles.length;i++){
			lineStrings[i] = maps.get(dbtitles[i])+"";
		}
		
		return lineStrings;
	}


	@Override
	public int getPageLines(int page) {
		// TODO Auto-generated method stub
		List list = getPage(page);
		
		if(list == null){
			return 0;
		}else{
			return list.size();
		}
	}


	@Override
	public int getTotalPage() {
		// TODO Auto-generated method stub
		int total = context.size()/pagecount;
		int left  = context.size()%pagecount;
		total-=1;
		if(left>0){
			total++;
		}
		return total;
	}

}
